// 获取全局应用程序实例对象
const app = getApp()

// 创建页面实例对象
Page({
    /**
     * 页面的初始数据
     */
    data: {
        category: [
            { name: '羽毛球', query: 'badminton' },
            { name: '乒乓球', query: 'table_tennis' },
            { name: '篮球', query: 'basketball' },
            { name: '足球', query: 'football' },
            { name: '台球', query: 'billiards' },
            { name: '网球', query: 'tennis' },
            { name: '游泳', query: 'swim' },
            { name: '健身', query: 'bodybuilding' },
            { name: '真冰溜冰', query: 'ice_skating' },
            { name: '滑雪', query: '壁球' },
            { name: '攀岩', query: 'rock_climbing' },
            { name: '保龄球', query: 'bowling' },
            { name: '排球', query: 'volleyball' },
            { name: '娱乐', query: 'entertainment' },
            { name: '其他', query: 'other' }
        ],
        // distance: ['距离最近'],
        categoryIndex: '',
        query: '',
        type: [
            { 'name': '倒序', 'type': 'desc' },
            { 'name': '正序', 'type': 'asc' }
        ],
        number: [{}, {}],
        typeIndex: '0',
        hasMore: false,
        loading: true,
        list: [],
        pagination: '',
        lat: '',
        lng: '',
        noContent: false
    },

    /**
     * 获取场馆列表
     */

    getData(type, sortType) {
        app.yds.getClubList(type, sortType, this.data.lat, this.data.lng)
            .then(res => {
                if (res && res.code == 0) {
                    if (!res.data.l.length) {
                        this.setData({ noContent: true, hasMore: false, loading: false });
                        wx.hideToast();
                        return;
                    }
                    this.setData({ loading: false, number: [{}] });
                    if (res.data.l.length >= app.yds.pageSize && res.data.pagination.totalItems != app.yds.pageIndex * app.yds.pageSize) {
                        this.setData({ hasMore: true, loading: true, noContent: false });
                    } else {
                        this.setData({ hasMore: false, loading: false, noContent: false });
                    }
                    this.setData({ list: this.data.list.concat(res.data.l) });
                    !this.data.pagination ? this.setData({ pagination: res.data.pagination }) : '';
                    wx.hideToast();
                    wx.stopPullDownRefresh();
                } else {
                    this.wetoast.toast({
                        duration: 2000,
                        title: res.msg,
                        img: '../../images/warn.png'
                    })
                }
            });
    },

    /**
     * picker运动类型选择器筛选
     */

    categoryPickerChange(e) {
        if (this.data.categoryIndex == e.detail.value) return;
        this.setData({ categoryIndex: e.detail.value, list: [], pagination: '' });
        app.wechat.loadingToast();
        this.getData(this.data.category[e.detail.value].query, 'asc');
    },

    /**
     * picker排序类型选择器筛选
     */

    // sortPickerChange(e) {
    //     if (this.data.typeIndex == e.detail.value) return;
    //     this.setData({ typeIndex: e.detail.value })
    //     this.getData(this.data.query, this.data.type[e.detail.value].type)
    //     app.wechat.loadingToast();
    // },


    /**
     * 上拉加载更多
     */

    handleLoadMore() {
        if (!this.data.hasMore) return;
        this.setData({ hasMore: false });
        app.yds.pageIndex = app.yds.pageIndex + 1;
        this.getData(this.data.query, 'asc')
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        new app.WeToast();
        app.yds.pageIndex = 1;
        this.setData({ query: option.query })         
        for (let i = this.data.category.length - 1; i >= 0; i--) {
            if(this.data.category[i].query == option.query){
                this.setData({ categoryIndex: i })         
            }
        }

        app.wechat.getLocation()
            .then(res => {
                let sortType = 'asc';
                this.setData({ lat: res.latitude, lng: res.longitude });
                this.getData(this.data.query, sortType)
            });
        // TODO: onLoad
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        // TODO: onReady
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // TODO: onShow
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        // TODO: onHide
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // TODO: onUnload
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    // onPullDownRefresh() {
    //     this.setData({ list: [], pagination: '', number: [{}, {}], loading: true })
    //     app.yds.pageIndex = 1;
    //     this.getData('distance', 'desc');
    //     // TODO: onPullDownRefresh
    // }
})
